package com.aiatomic.countlib;

import android.util.Log;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

class WriteHelper {
    public static void sendData(OutputStream outputStream, String data) throws IOException {
        Log.e("WriteHelper", "sendData" + data);
        byte[] body = null;
        body = data.getBytes(StandardCharsets.UTF_8);
        if (body == null) {
            return;
        }

        int length = 4 + body.length;
        ByteBuffer buffer = ByteBuffer.allocate(length);
        buffer.order();
        buffer.putInt(body.length);
        buffer.put(body);
        if (outputStream != null) {
            outputStream.write(buffer.array());
            outputStream.flush();
        }
    }
}
